package com.shop.cloud.common.email.util;

import com.shop.cloud.common.email.config.EmailConfigProperties;
import io.github.biezhi.ome.OhMyEmail;
import io.github.biezhi.ome.SendMailException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Slf4j
@Configuration
@AllArgsConstructor
public class EmailUtils {
	private final EmailConfigProperties emailConfigProperties;


	/**
	 * 单发
	 * @param to
	 * @param title
	 * @param content
	 * @throws SendMailException
	 */
	public void sendEmail(String to, String title, String content) throws SendMailException {
		OhMyEmail.subject(title)
				.from(emailConfigProperties.getSiteName())
				.to(to)
				.html(content)
				.send();
		log.info("email: {} send success", to);
	}

	/**
	 * 群发
	 * @param to
	 * @param title
	 * @param content
	 * @throws SendMailException
	 */
	public void sendEmail(String[] to, String title, String content) {
		for (String str : to) {
			try {
				OhMyEmail.subject(title)
						.from(emailConfigProperties.getSiteName())
						.to(str)
						.html(content)
						.send();
				log.info("email: {} send OK", str);
			} catch (SendMailException e) {
				log.info("email: {} send error：{}", str, e.getMessage());
			}
		}
	}
}
